package com.dhecp.project.storage.controller;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.config.Global;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.layui.GridColumnsPropertyModel;
import com.dhecp.core.utils.ExportExcelUtils;
import com.dhecp.core.utils.PdfUtil;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.api.util.ApiConfig;
import com.dhecp.project.business.domain.FbaPlanOrder;
import com.dhecp.project.business.domain.FbaPlanOrderDetail;
import com.dhecp.project.business.domain.FbaShipmentDetail;
import com.dhecp.project.business.service.FbaPlanOrderService;
import com.dhecp.project.logstics.controller.FbaBoxController;
import com.dhecp.project.logstics.domain.FbaBox;
import com.dhecp.project.logstics.mapper.FbaShipmentDetailMapper;
import com.dhecp.project.logstics.service.FbaBoxService;
import com.dhecp.project.storage.domain.HwFinalstatement;
import com.dhecp.project.storage.domain.HwFinalstatementdetail;
import com.dhecp.project.storage.domain.HwTailprogram;
import com.dhecp.project.storage.domain.HwTailprogramdetail;
import com.dhecp.project.storage.enums.TailprogramEnum;
import com.dhecp.project.storage.mapper.HwFinalstatementdetailMapper;
import com.dhecp.project.storage.mapper.HwTailprogramMapper;
import com.dhecp.project.storage.mapper.HwTailprogramdetailMapper;
import com.dhecp.project.storage.service.HwTailprogramService;
import com.dhecp.project.storage.service.HwTailprogramdetailService;
import com.dhecp.project.system.domain.SysDictionaryForExport;
import com.dhecp.project.system.domain.SysFilesInfo;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import com.dhecp.project.system.service.SysFilesInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.dhecp.project.common.controller.BaseModuleController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 海外仓尾程单(HwTailprogram)控制层
 *
 * @author fxz
 * @since 2023-09-08 10:07:45
 */
@Controller
@RequestMapping("/storage/hwTailprogram")
public class HwTailprogramController extends BaseModuleController{

    private String prefix = "storage/hwTailprogram";
    private String moduleTable = ModuleTable.HW_TAILPROGRAM;
    private String moduleTable1 = ModuleTable.HW_TAILPROGRAMDETAIL;
    private String moduleTable2 = ModuleTable.FBA_BOX;

    @Autowired
    private HwTailprogramService hwTailprogramService;

    @Autowired
    private HwTailprogramMapper hwTailprogramMapper;

    @Autowired
    private GridColumnsPropertyModel gridColumnsPropertyModel;

    @Autowired
    private HwTailprogramdetailService hwTailprogramdetailService;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

    @Autowired
    private HwFinalstatementdetailMapper hwFinalstatementdetailMapper;

    @Autowired
    private FbaPlanOrderService fbaPlanOrderService;

    @Autowired
    private FbaBoxService fbaBoxService;

    @Autowired
    private SysFilesInfoService sysFilesInfoService;

    @Autowired
    private HwTailprogramdetailMapper hwTailprogramdetailMapper;

    @Autowired
    private FbaShipmentDetailMapper fbaShipmentDetailMapper;


    /**
     * @Description: 打开查询列表页
     * @Param: [params]
     * @return: org.springframework.web.servlet.ModelAndView
     * @Author: hlf
     * @date: 2022-08-15 15:42
     */
    @RequiresPermissions("storage:hwTailprogram:view")
    @GetMapping()
    public ModelAndView view(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        params.put("entity", new HwTailprogram()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
        params.put("prefix", prefix + "/hwTailprogram"); //页面路径
        params.put("toolBarWidth", 0);
        params.put("checkBox", "yes");

        ModelAndView mv = this.openListPage(params);
        Object importFields = dictionaryForFieldService.queryFieldFrontImport(params); //导入模板字段
        mv.addObject("importFields", importFields);	//导入模板字段
        mv.addObject("storage", "hwTailprogram");
        mv.addObject("ali1688EcpApiUrl", ApiConfig.getAli1688EcpApiUrl());
        return mv;
    }

    /**
     * @Description: 查询页 列表数据
     * @Param: [params]
     * @return: com.dhecp.common.core.page.TableDataInfo
     * @Author: fxz
     * @date: 2023-09-08 15:43
     */
    @RequiresPermissions("storage:hwTailprogram:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        List<HwTailprogram> list = new ArrayList<HwTailprogram>();
        try {
            list = hwTailprogramService.queryList(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());
        }
        return getDataTable(list);
    }

    /**
     * @Description: 查询页 子列表数据
     * @Param: [params]
     * @return: com.dhecp.common.core.page.TableDataInfo
     * @Author: fxz
     * @date: 2023-09-08 15:43
     */
    @RequiresPermissions("storage:hwTailprogram:list")
    @GetMapping("/sonlist")
    @ResponseBody
    public AjaxResult sonlist(@RequestParam Map<String, Object> params) {

        try {
            AjaxResult ajaxResult = new AjaxResult();
            Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
            Map<String, Object> params1 = new HashMap<String, Object>();
            Map<String, Object> mapList = new HashMap<>();
            Map<String, Object> stringObjectMap = new HashMap<>();
            params.put("toolBarWidth", 0);
            params.put("moduleTable", moduleTable);
            params.put("entity", new HwTailprogramdetail()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
            params.put("tpcode",params.get("code"));
            params.put("guid",params.get("guid"));
            HwTailprogram hwTailprogram = hwTailprogramService.queryFirstRecord(params);
            mapList.put("hwTailprogram", hwTailprogram);
            params.remove("guid");
            if (hwTailprogram.getCreationMethod().equals("手动")){
                mapList.put("sonList", hwTailprogramdetailService.queryList(params));
            } else {
                mapList.put("sonList", hwTailprogramdetailService.queryAutoList(params));
            }
            stringObjectMap = gridColumnsPropertyModel.queryGridColumnsPropertyModel(params);
            mapList.put("fields", TypeUtils.mapToJson(dictionaryForFieldMap));
            mapList.put("gridColumns", stringObjectMap);
            ajaxResult = AjaxResult.success(mapList);
            return ajaxResult;
        } catch (Exception e) {
            return AjaxResult.error(this.getExceptionMessage(e));
        }
    }

    /**
     * @Description: 修改尾程方式
     * @Param: [params]
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: fxz
     * @date: 2023-09-08 23:55
     */
    @RequiresPermissions("storage:hwTailprogram:view")
    @Log(title = "修改尾程方式", businessType = BusinessType.UPDATE)
    @PostMapping("/updateTailProgram")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult updateState(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = hwTailprogramService.updateTailProgram(params);
        } catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }


    /**
     * @Description: 修改渠道代码
     * @Param: [params]
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: fxz
     * @date: 2023-09-08 23:55
     */
    @RequiresPermissions("storage:hwTailprogram:view")
    @Log(title = "修改渠道代码", businessType = BusinessType.UPDATE)
    @PostMapping("/updateChannel")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult updateChannel(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = hwTailprogramService.updateChannel(params);
        } catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }


    /**
     * @Description: 修改附加费
     * @Param: [params]
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: fxz
     * @date: 2023-09-08 23:55
     */
    @RequiresPermissions("storage:hwTailprogram:view")
    @Log(title = "修改附加费", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAddition")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult updateAddition(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = hwTailprogramService.updateAddition(params);
        } catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * @Description: 修改状态
     * @Param: [params]
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: fxz
     * @date: 2023-09-08 23:55
     */
    @RequiresPermissions("storage:hwTailprogram:view")
    @Log(title = "修改状态", businessType = BusinessType.UPDATE)
    @PostMapping("/updateStateName")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult updateStateName(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = hwTailprogramService.updateStateName(params);
        } catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }


    /**
     * @Description: 删除
     * @Param: [params]
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: fxz
     * @date: 2023-09-08 23:55
     */
    @RequiresPermissions("storage:hwTailprogram:view")
    @Log(title = "修改状态", businessType = BusinessType.UPDATE)
    @PostMapping("/delete")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult delete(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = hwTailprogramService.delete(params);
        } catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * @Description: 数据导出Excel
     * @Param: [params]
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: fxz
     * @date: 2023-09-08 23:43
     */
    @RequiresPermissions("storage:hwTailprogram:export")
    @Log(title = "海外尾程数据导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult export(@RequestParam Map<String, Object> params) throws Exception {
        params.put("moduleTable", moduleTable);
        params.put("entity", new HwTailprogram());

        List<HwTailprogram> list = hwTailprogramService.queryList(params);

        params.put("getFieldAttributesType", "export");
        List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<HwTailprogram> util = new ExportExcelUtils<HwTailprogram>(moduleTable, new HwTailprogram());
        return util.exportExcel(list, fieldList, "海外尾程数据", "export");
    }

    /**
     * @Description: 海外尾程导入模板
     * @Param: [params]
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: fxz
     * @date: 2023-09-08 23:43
     */
    @RequiresPermissions("storage:hwTailprogram:export")
    @Log(title = "海外尾程导入模板", businessType = BusinessType.EXPORT)
    @GetMapping("/exporttemplate")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult exporttemplate(@RequestParam Map<String, Object> params) throws Exception {
        params.put("moduleTable", moduleTable);
        params.put("entity", new HwTailprogram());
        params.put("getFieldAttributesType", "template");
        List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<HwTailprogram> util = new ExportExcelUtils<HwTailprogram>(moduleTable, new HwTailprogram());
        return util.exportExcel(null, fieldList, "海外尾程导入模板", "template");
    }


    /**
     * 导入并直接回填追踪编号单据
     * @param params 必要参数：对应实体类的json格式字符串（导入的字段用户内容）
     * @return
     * @throws Exception
     * @author fxz
     */
    @Log(title = "追踪编号导入", businessType = BusinessType.EXPORT)
    @PostMapping("/importdatapost")
    @ResponseBody
    public AjaxResult importData(@RequestParam Map<String, Object> params) throws Exception {
        params.put("moduleTable", moduleTable);

        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= hwTailprogramService.importData(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }


    /**
     * @Description: 查询页 列表数据
     * @Param: [params]
     * @return: com.dhecp.common.core.page.TableDataInfo
     * @Author: fxz
     * @date: 2023-06-08
     */
    @RequiresPermissions("storage:hwTailprogram:list")
    @GetMapping("/hwTailprogramList")
    @ResponseBody
    public TableDataInfo hwTailprogramList(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable1);
        List<HwTailprogram> list = new ArrayList<HwTailprogram>();
        List<HwFinalstatementdetail> hwFinalstatementdetailList = hwFinalstatementdetailMapper.queryListTpcode(params.get("bid").toString());
        StringBuffer sb = new StringBuffer();
        for (HwFinalstatementdetail hwFinalstatementdetail : hwFinalstatementdetailList){
            sb.append(hwFinalstatementdetail.getBid()).append(",");
        }
        String codereconcile = " (t1.stateName = '已审核' AND t1.stockOutCode IS NOT NULL)";
        if (sb.length() != 0){
            String sb1 = sb.toString().substring(0,sb.length()-1);
            codereconcile = "t1.bid in ('" + sb1.replace(",", "','") + "')";
        }
        params.put("codereconcile", codereconcile);

        try {
            list = hwTailprogramService.queryListSelect(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());
        }
        return getDataTable(list);
    }


    /**
     *@Description: 查询已选择的商品
     *@Param: selectedUser ：已选中的商品编号字符串，示例：0000,1001,1002
     *@return: com.dhecp.common.core.page.TableDataInfo
     *@Author: fxz
     *@date: 2022-08-18 17:13
     */
    @GetMapping("/listselect")
    @ResponseBody
    public TableDataInfo userInfoSelectListJson(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable1);
        List<HwTailprogram> list = new ArrayList<HwTailprogram>();
        if (params.get("selectedUser") != null && params.get("selectedUser") != ""){
            String wherestrhand = "t1.shipmentid in ('" + params.get("selectedUser").toString().replace(",", "','") + "')";
            params.put("wherestrhand", wherestrhand);
            try{
                list = hwTailprogramService.queryListSelect(params);
            }catch(Exception e){
                return getDataTable(this.getExceptionMessage(e).toString());
            }
        }else{
            return getDataTable("请传入已选中的商品编码字符串（selectedUser），取值示例：0000,1001,1002");
        }
        return getDataTable(list);
    }


    /**
     * @Description: 修改尾程出库单
     * @Param: [params]
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: fxz
     * @date: 2023-09-08 23:55
     */
    @RequiresPermissions("storage:hwTailprogram:view")
    @Log(title = "修改尾程出库单", businessType = BusinessType.UPDATE)
    @PostMapping("/updateStockOutCode")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult updateStockOutCode(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = hwTailprogramService.updateStockOutCode(params);
        } catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 打印商品标页面
     * @param code 头程计划单号
     * @return
     */
    @GetMapping("/toPrintGoods")
    public ModelAndView toPrintGoods(String code) throws Exception {


        ModelAndView mv = new ModelAndView();
        mv.setViewName(prefix + "/toPrintGoods");

        List<FbaBoxController.ToPrintGoodsDto.RowDto> rows = new ArrayList<FbaBoxController.ToPrintGoodsDto.RowDto>();
        Map<String, Object> params2 = new HashMap<String, Object>();
        params2.put("moduleTable",moduleTable1);
        params2.put("tpcode",code);
        //查询尾程单数据
        List<HwTailprogramdetail> hwTailprogramdetailList = hwTailprogramdetailMapper.findHwTailprogramdetail(params2);
        List<HwTailprogramdetail> hwTailprogramdetailNew = JSON.parseArray(hwTailprogramdetailList.get(0).getProductinfo(), HwTailprogramdetail.class);
        for (HwTailprogramdetail hwTailprogramdetail : hwTailprogramdetailNew) {
            String guid = hwTailprogramdetail.getCode();
            String code2 = hwTailprogramdetail.getCommodityCode();
            String name = hwTailprogramdetail.getName();
            String fnsku = hwTailprogramdetail.getFnsku();
            String asin = hwTailprogramdetail.getAsin();
            String sku = hwTailprogramdetail.getSku();
            String num = hwTailprogramdetail.getNumPlanDeliver();
            String signCode = hwTailprogramdetail.getSignCode();
            String code3 = code;

            //查询头程货件详情
            params2.put("guid",hwTailprogramdetail.getCode());
            FbaShipmentDetail fbaShipmentDetail = fbaShipmentDetailMapper.queryFirstByGuid(params2);

            List<SysFilesInfo> listSysFilesInfo = sysFilesInfoService.listSysFilesInfoByFileMark(fbaShipmentDetail.getPdGuid());

            List<FbaBoxController.ToPrintGoodsDto.RowDto.FileDto> fileToumingbiaoArr = new ArrayList<FbaBoxController.ToPrintGoodsDto.RowDto.FileDto>();
            for(SysFilesInfo sysFilesInfo : listSysFilesInfo) {
                String link = sysFilesInfo.getFilePath();
                fileToumingbiaoArr.add(new FbaBoxController.ToPrintGoodsDto.RowDto.FileDto(link));

            }

            rows.add(new FbaBoxController.ToPrintGoodsDto.RowDto(guid, code2, name, fnsku, asin, sku, num, code3, signCode,fileToumingbiaoArr));
        }
        FbaBoxController.ToPrintGoodsDto dto = new FbaBoxController.ToPrintGoodsDto(code, rows);
        mv.addObject("dto", dto);
        return mv;
    }

    /**
     * 获取商品标打印信息
     * @param params
     * @return
     */
    @PostMapping("/getGoodsPrintInfo")
    @ResponseBody
    public AjaxResult getGoodsPrintInfo(String guid,String code3){

        AjaxResult ajaxResult = new AjaxResult();
        try {
            //查询头程货件详情
            Map<String, Object> params2 = new HashMap<String, Object>();
            params2.put("guid",guid);
            FbaShipmentDetail fbaShipmentDetail = fbaShipmentDetailMapper.queryFirstByGuid(params2);
            if (fbaShipmentDetail == null){
                return AjaxResult.error("单据不存在,不允许打印!");
            }
            FbaPlanOrderDetail fbaPlanOrderDetail = fbaPlanOrderService.queryFbaPlanOrderDetailByGuid(fbaShipmentDetail.getPdGuid());
            if (fbaPlanOrderDetail == null){
                return AjaxResult.error("单据不存在,不允许打印!");
            }

            // 生成PDF到指定目录下
            String dir = Global.getProfile();
            String pdfFileName = "/" + guid + ".pdf";

            String nameEn = fbaPlanOrderDetail.getNameGoodsEn();
            String nameCn = fbaPlanOrderDetail.getName();

            String msg = fbaPlanOrderDetail.getFnsku();
            Integer num = fbaPlanOrderDetail.getNumPlanDeliver();
            PdfUtil.GoodsDto dto = new PdfUtil.GoodsDto(nameEn, nameCn, msg, num);

            PdfUtil.generateGoods(dir, pdfFileName, dto, num);

            Map<String, Object> params = new HashMap<String, Object>();
            params.put("moduleTable", moduleTable2);
            params.put("entity", new FbaBox());
            params.put("code",code3);

            ajaxResult = AjaxResult.success(dto);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 打印透明标状态
     * @param params
     * @return
     */
    @PostMapping("/doPrintTMB")
    @ResponseBody
    public AjaxResult doPrintTMB(String code){

        AjaxResult ajaxResult = new AjaxResult();
        try {
            Map<String, Object> paramBox = new HashMap<String, Object>();
            paramBox.put("moduleTable", moduleTable);
            paramBox.put("entity", new HwTailprogram());
            paramBox.put("bid",code);
            HwTailprogram hwTailprogram = hwTailprogramMapper.queryFirstRecord(paramBox);

            if (hwTailprogram.getStatePrintName().equals(TailprogramEnum.StateName.PRINTNOHAVE.value)){
                return ajaxResult = AjaxResult.error("选择单据中商品标未打印,请核实后在操作!");
            }

            ajaxResult = AjaxResult.success();
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 获取所有商品标打印信息
     * @param params
     * @return
     */
    @PostMapping("/getGoodsPrintInfoAll")
    @ResponseBody
    public AjaxResult getGoodsPrintInfoAll(String code){

        AjaxResult ajaxResult = new AjaxResult();
        try {
            Map<String, Object> params2 = new HashMap<String, Object>();
            params2.put("bid",code);
            HwTailprogram hwTailprogram = hwTailprogramMapper.queryFirstRecord(params2);
            params2.put("moduleTable",moduleTable1);
            params2.put("tpcode",code);
            //查询尾程单数据
            List<HwTailprogramdetail> hwTailprogramdetailList = hwTailprogramdetailMapper.findHwTailprogramdetail(params2);
            List<HwTailprogramdetail> hwTailprogramdetailNew = JSON.parseArray(hwTailprogramdetailList.get(0).getProductinfo(), HwTailprogramdetail.class);
            List<PdfUtil.GoodsDto> rows = new ArrayList<PdfUtil.GoodsDto>();
            for (HwTailprogramdetail hwTailprogramdetail : hwTailprogramdetailNew) {
                String nameEn = hwTailprogramdetail.getNameGoodsEn();
                String nameCn = hwTailprogramdetail.getName();

                String msg = hwTailprogramdetail.getFnsku();
                Integer num = Integer.valueOf(hwTailprogramdetail.getNumPlanDeliver());
                rows.add(new PdfUtil.GoodsDto(nameEn, nameCn, msg, num));
            }

            hwTailprogram.setStatePrintName(TailprogramEnum.StateName.PRINTONE.value);
            hwTailprogramMapper.update(hwTailprogram);
            ajaxResult = AjaxResult.success(rows);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 批量透明标状态
     * @param params
     * @return
     */
    @PostMapping("/doPrintTMBAll")
    @ResponseBody
    public AjaxResult getDoPrintTMBAll(String code){

        AjaxResult ajaxResult = new AjaxResult();
        try {
            Map<String, Object> paramHw = new HashMap<String, Object>();
            paramHw.put("moduleTable", moduleTable);
            paramHw.put("entity", new HwTailprogram());
            paramHw.put("bid",code);
            HwTailprogram hwTailprogram = hwTailprogramMapper.queryFirstRecord(paramHw);

            if (hwTailprogram.getStatePrintName().equals(TailprogramEnum.StateName.PRINTONE.value)){
                hwTailprogram.setStatePrintName("商品标和透明标已打印");
            }
            if (hwTailprogram.getIsfmb() != null){
                if (hwTailprogram.getIsfmb() == 0){
                    return ajaxResult = AjaxResult.error("选择单据中无透明标,无需打印!");
                }
            }
            if (hwTailprogram.getStatePrintName().equals(TailprogramEnum.StateName.PRINTNOHAVE.value)){
                return ajaxResult = AjaxResult.error("选择单据中商品标未打印,请核实后在操作!");
            }
            hwTailprogramMapper.update(hwTailprogram);
            ajaxResult = AjaxResult.success();
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * @Description: 打开制单页
     * @Param: nid 必要参数：单据编码（新增时传默认值 0 ，修改、查看时传单据实际值）
     * @return: org.springframework.web.servlet.ModelAndView
     * @Author: hlf
     * @date: 2022-08-15 15:42
     */
    @RequiresPermissions("storage:hwTailprogram:view")
    @GetMapping("/edit")
    public ModelAndView editPage(@RequestParam Map<String, Object> params) {
        if (params.get("nid") == null || params.get("nid") == "") {
            params.put("nid", "0");
        }
        params.put("moduleTable", moduleTable);
        params.put("entity", new HwTailprogram()); //用于获取实体属性名取代对应字典字段名，解决大小写敏感问题
        params.put("moduleUrl", prefix); //页面上级路径
        params.put("prefix", prefix + "/hwTailprogram_edit"); //页面路径
        Subject userSubject = ShiroUtils.getSubject();
        int enableEdit = 0;
        boolean vType = false;
        if (params.get(enableEdit) == null || "1".equals(params.get(enableEdit).toString())) {
            if (userSubject.isPermitted("storage:hwTailprogram:edit")) { //当前登录用户主体是否有修改的权限
                enableEdit = 1;
            }
        }

        try {
            Map<String, Object> headData = new HashMap<String, Object>();        //表头的业务数据
            HwTailprogram entityData = hwTailprogramMapper.queryFirstRecord(params);
            headData = TypeUtils.entityToMap(entityData);
            params.put("headData", headData); //表头的业务数据

            params.put("checkBox", "yes"); //生成表格gridColumns时包含复选
            ModelAndView mv = this.openEditPage(params);
            mv.addObject("hwTailprogram", entityData);
            if (entityData != null) {
                Map<String, Object> params2 = new HashMap<String, Object>();
                params2.put("moduleTable", moduleTable1);
                params2.put("tpcode",entityData.getBid());
                if (entityData.getCreationMethod().equals("手动")){
                    mv.addObject("List", hwTailprogramdetailService.queryList(params2));
                } else {
                    mv.addObject("List", hwTailprogramdetailService.queryAutoList(params2));
                }
            }
            //取子件列表网格
            params.put("moduleTable", moduleTable1);
            params.put("entity", new HwTailprogramdetail()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
            params.put("readOnly", vType);//生成列是否可编辑
            params.put("toolBarWidth", 0);//操作区
            params.put("getFieldAttributesType", "pageingrid");//pageingrid：网格字段
            mv.addObject("gridColumnsZJ", gridColumnsPropertyModel.queryGridColumnsPropertyModel(params));

            return mv;
        } catch (Exception e) {
            return this.gotoModelAndViewErrorPage(e.getMessage());    //详细信息
        }

    }

}

